package com.huan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huan.VO.AdminUserRouterVo;
import com.huan.VO.MenuVo;
import com.huan.domain.ResponseResult;
import com.huan.domain.entity.Menu;
import com.huan.domain.entity.Tag;
import com.huan.mapper.MenuMapper;
import com.huan.service.MenuService;
import com.huan.utils.BeanCopyUtil;
import com.huan.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 菜单权限表(Menu)表服务实现类
 *
 * @author makejava
 * @since 2023-06-24 08:29:03
 */
@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

@Autowired
private MenuMapper menuMapper;
    @Override
    public List<String> selectPermsByUserId(Long id) {
//        如果id为1，则返回所有的权限，如果不是则根据id进行查询权限并返回
        if (id == 1L){
            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(Menu::getMenuType,"C","F");
            List<Menu> list = list(wrapper);
            List<String> collect = list.stream()
                    .map(Menu::getPerms)
                    .collect(Collectors.toList());
            return collect;
        }
        return getBaseMapper().selectPermsByUserId(id);
    }

    @Override
    public AdminUserRouterVo selectRouterMenuTreeByUserId(Long id) {
        MenuMapper menuMapper = getBaseMapper();
        List<Menu> menus = null;
        if (SecurityUtils.isAdmin()){
            LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(Menu::getMenuType,"C","M");
            wrapper.eq(Menu::getStatus,"0");
            wrapper.eq(Menu::getDelFlag,"0");
            wrapper.orderByAsc(Menu::getOrderNum);
            menus = list(wrapper);
        }else{
            //否则  获取当前用户所具有的Menu
            menus = menuMapper.selectRouterMenuTreeByUserId(id);
        }
        List<Menu> finalMenus = menus;
        List<Menu> menuTree = menus.stream()
                .filter(menu -> menu.getParentId().equals(0L))
                .map(menu -> menu.setChildren(getChildren(menu, finalMenus)))
                .collect(Collectors.toList());


        AdminUserRouterVo routerVos = new AdminUserRouterVo().setMenus(menuTree);
        return routerVos;
    }

    @Override
    public ResponseResult mlist(String status, String menuName) {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Menu::getStatus,status);
        wrapper.in(Menu::getMenuName,menuName);
        wrapper.orderByAsc(Menu::getStatus,Menu::getOrderNum);
        List<Menu> menuList = list(wrapper);

        return ResponseResult.okResult(menuList);
    }

    @Override
    public ResponseResult addList(Menu menu) {
        save(menu);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getMenu(Long id) {
        Menu menu = getById(id);

        return ResponseResult.okResult(menu);
    }

    @Override
    public ResponseResult udMenu(Menu menu) {
        if (menu.getParentId() == 0){
            return ResponseResult.errorResult(500,"修改菜单'写博文'失败，上级菜单不能选择自己");
        }
        updateById(menu);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteMenu(Long menuId) {
        menuMapper.deleteById(menuId);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTreeMenu() {
        List<Menu> list = list(null);
//        List<MenuVo> menuVos = BeanCopyUtil.resultAllVo(list, MenuVo.class);
        List<Menu> voList = list.stream()
                .map(menu ->
                    menu.setChildren(Getchildren(menu, list))
                )
                .collect(Collectors.toList());
        List<MenuVo> menuVos = BeanCopyUtil.resultAllVo(voList, MenuVo.class);
        List<MenuVo> menuVoList = menuVos.stream()
                .map(menuVo -> menuVo.setLabel(getlabel(menuVo, list)))
                .collect(Collectors.toList());

        return ResponseResult.okResult(menuVoList);
    }

    private List<Menu> Getchildren(Menu menu, List<Menu> menuList) {
        List<Menu> collect = menuList.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m -> m.setChildren(Getchildren(m, menuList)))
                .collect(Collectors.toList());
        return collect;
    }

    private String getlabel(MenuVo menuVo, List<Menu> menuList) {
        for (Menu menu : menuList) {
            if (menu.getId() == menuVo.getId()){
                return menu.getMenuName();
            }
        }
        return null;
    }



    private List<Menu> getChildren(Menu menu, List<Menu> menus) {
        List<Menu> childrenList = menus.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))
                .map(m->m.setChildren(getChildren(m,menus)))
                .collect(Collectors.toList());
        return childrenList;
    }




















}

